package com.tarena.luban.demo.gateway.handler;

import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

@Order(-1)//保证优先级低于默认的ResponseStatusExceptionHandler 提前执行,然后让
//状态码附加
@Component
public class GlobalErrorHandler implements ErrorWebExceptionHandler {
    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        //TODO 需要判断获取一下状态值, 判断异常类型,如果是自定义的业务异常,
        //如果是系统异常
        ServerHttpResponse response = exchange.getResponse();
        String authError="您的认证验证失败";
        return response.writeWith(Mono.fromSupplier(
                () -> response.bufferFactory().wrap(authError.getBytes(StandardCharsets.UTF_8))
        ));
    }
}
